package com.sob.bountyhunter.entidades;

import java.awt.Rectangle;

import org.newdawn.slick.Animation;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.util.pathfinding.Mover;

import com.sob.bountyhunter.Art;
import com.sob.bountyhunter.Mapa;

public class Entidad implements Mover {
	protected float xvel = 0.2f;
	protected float yvel = 0.2f;
	protected float x,y, xinc,yinc;
	protected float z;
	
	protected float xdir, ydir;
	protected float friccion = 1f;
	protected boolean tieneFriccion = true;
	protected float cx, cy;
	protected Image sprite;
	
	protected float sprSize=1f;
	
	protected int dirAnim=0;
	protected boolean updateAnim=false;
	protected Animation []animacion;
	protected int animFrame=0;
	
	protected boolean hMirror = false;
	public Color tinte; 
	
	/*
	public enum Direccion{
		DIR_ABAJO, DIR_IZQUIERDA, DIR_ARRIBA, DIR_DERECHA;
	}*/
	
	public Entidad(String sf) throws SlickException{
		sprite=new Image(sf,new Color(0xFF00FF));
		tinte=new Color(Color.white);		
	}
	public Entidad() throws SlickException{
		tinte=new Color(Color.white);		
		
	}

	/*
	 * No se usa por ahora
	 *
	@Deprecated
	public Entidad(String sf, int sw, int sh, int time) throws SlickException{
		tinte=new Color(Color.white);		
				
		SpriteSheet archivo=new SpriteSheet(sf, sw, sh, new Color(0xFF00FF));
		animacion=new Animation[4];
		for(int i=0;i<4;i++){
			animacion[i]=new Animation();
			animacion[i].setAutoUpdate(false);
			for(int j=0;j<archivo.getWidth()/sw;j++){
				animacion[i].addFrame(archivo.getSubImage(j, i), time);				
			}
		}
		
		sprite=animacion[dirAnim].getCurrentFrame();
		
	}
	*/
	
	public void setAnimacion(String name){
		animacion=Art.getAnim(name);
	}
	
	public void render(int xOff, int yOff)
	{
		if(sprite!=null){
			Image t;
			
			t=sprite.getFlippedCopy(hMirror, false);
				
			t.draw((x-(cx*sprSize))-xOff,(y-(cy*sprSize))-yOff,sprSize, tinte);
		}
	}
	
	
	public void update(GameContainer gameContainer, int deltaTime, Mapa map)
	{
		int radio = 15;
		
		//Mira si estan bloqueada la casilla en la que te vas a mover
		if (!map.isBlocked(x+xinc+(radio*xdir), y) &&
			!map.isBlocked(x+xinc+(radio*xdir), y+radio) &&
			!map.isBlocked(x+xinc+(radio*xdir), y-radio)){	
			
			boolean coli=false;			
			for(Entidad e: map.entidades){
				if(!e.equals(this)){
					if(colision(e, x+xinc, y)){
						coli=true;
						break;
					}
				}
			}
			
			if(!coli){
				x+=xinc;
				
				//Direccion en la que se mueve
				if(xinc<0) xdir=-1; else if(xinc>0) xdir=1; else xdir=0;
				
				//Aplica friccion
				if(tieneFriccion) {
					xinc+=(friccion*deltaTime)*(-xdir);
					if(xinc<0 && xdir==1) xinc=0;
					if(xinc>0 && xdir==-1) xinc=0;
				}
			}
		}
		
		//Lo mismo pero en el eje y
		if (!map.isBlocked(x, y+yinc+(radio*ydir)) &&
			!map.isBlocked(x-radio, y+yinc+(radio*ydir)) &&
			!map.isBlocked(x+radio, y+yinc+(radio*ydir))){
			
			boolean coli=false;			
			for(Entidad e: map.entidades){
				if(!e.equals(this)){
					if(colision(e, x, y+yinc)){
						coli=true;
						break;
					}
				}
			}

			
			if(!coli){
				y+=yinc;
				
				if(yinc<0) ydir=-1; else if(yinc>0) ydir=1; else ydir=0; 
	
				if(tieneFriccion) {
					yinc+=(friccion*deltaTime)*(-ydir);
					if(yinc<0 && ydir==1) yinc=0;
					if(yinc>0 && ydir==-1) yinc=0;
				}
			}
		}
		
		//Guardamos el orden de dibujado 
		z=y;
		
		if(animacion!=null){
			if(updateAnim){
				animacion[dirAnim].update(deltaTime);
				animFrame=animacion[dirAnim].getFrame();
			}
			sprite=animacion[dirAnim].getCurrentFrame();
			/*else{
				animacion[dirAnim].setCurrentFrame(0);
				sprite=animacion[dirAnim].getCurrentFrame();
			}*/
		}
	}

	
	public boolean colision(Entidad e, float px, float py){
		boolean res=false;
		
		res=this.getCajaColision(px, py).intersects(e.getCajaColision());
		
		return res;
	}
	
	//Getters y Setters	
	public Rectangle getCajaColision()
	{	
		return null;
	}
	public Rectangle getCajaColision(float px, float py)
	{
		return null;
	}
	
	public void setPos(float px, float py){
		x=px;
		y=py;
	}
	public void setPosTile(int tx, int ty){
		x=tx*Mapa.TILE_SIZE;
		y=ty*Mapa.TILE_SIZE;
	}

	public int getXTile(){
		return (int)x/Mapa.TILE_SIZE;
	}
	public int getYTile(){
		return (int)y/Mapa.TILE_SIZE;
	}
	
	public float getX(){
		return x;
	}
	public float getY(){
		return y;
	}		
	public float getZ(){
		return z;
	}
	
	public float getSize(){
		return sprSize;
	}
	public void setSize(float s){
		sprSize=s;
	}
}
